using System;

namespace Voodoo.Sauce.Internal.Ads
{
	public class BannerAd
	{
		private const string TAG = "BannerAd";

		public Action<float> OnAdShown;

		public Action OnAdClicked;

		public Action OnAdFailed;

		protected string _adUnit;

		private string _mediation;

		protected bool _isShowing;

		public static event Action OnBannerShowing;

		public static event Action OnBannerHiding;

		public BannerAd(string mediation, string adUnit)
		{
			VoodooLog.Log("BannerAd", "Creating banner for ad unit " + adUnit);
			_mediation = mediation;
			_adUnit = adUnit;
		}

		public virtual void Show()
		{
			_isShowing = true;
			DebugLog("Show");
		}

		public virtual void Hide()
		{
			_isShowing = false;
			DebugLog("Hide");
			if (BannerAd.OnBannerHiding != null)
			{
				BannerAd.OnBannerHiding();
			}
		}

		public virtual void Destroy()
		{
			_isShowing = false;
			DebugLog("Destroy");
			UnregisterEvents();
			if (BannerAd.OnBannerHiding != null)
			{
				BannerAd.OnBannerHiding();
			}
		}

		protected virtual void RegisterEvents()
		{
			DebugLog("RegisterEvents");
		}

		protected virtual void UnregisterEvents()
		{
			DebugLog("UnregisterEvents");
			OnAdShown = null;
			OnAdClicked = null;
			OnAdFailed = null;
		}

		protected void OnAdFailedCallback(string errorMsg)
		{
			DebugLog("OnAdFailedCallback, error: " + errorMsg);
			if (OnAdFailed != null)
			{
				OnAdFailed();
			}
			if (BannerAd.OnBannerHiding != null)
			{
				BannerAd.OnBannerHiding();
			}
		}

		protected void OnAdLoadedCallback(float height)
		{
			DebugLog("OnAdLoadedCallback, height: " + height);
			if (OnAdShown != null)
			{
				OnAdShown(height);
			}
			OnAdShown = null;
			if (_isShowing && BannerAd.OnBannerShowing != null)
			{
				BannerAd.OnBannerShowing();
			}
		}

		protected void OnAdClickedCallback()
		{
			DebugLog("OnAdClickedCallback");
			if (OnAdClicked != null)
			{
				OnAdClicked();
			}
		}

		private void DebugLog(string message)
		{
			VoodooLog.Log("BannerAd", $"BannerAd - Mediation: {_mediation} - AdUnit: {_adUnit} - {message}");
		}
	}
}
